<?php
load_info_var($cf_controller);											//加载smarty变量
load_file('channel_class',LIB_PATH);
$main = new Channel($db);
$id = isset($_POST['id']) ? intval($_POST['id']) : intval($_GET['id']);
$return_page = $cf_pre_page ? $cf_pre_page : $cf_self_page;
$apkimg_path = SITE_ADDR.'public/upload/apkimage/';
switch($cf_action){
	case "index":
		$smarty->mypage($cf_controller, $cf_action, 0);             
        $skey = isset($_POST['skey']) ? trim($_POST['skey']) : urldecode(trim($_GET['skey']));
		$skey2 = isset($_POST['skey2']) ? trim($_POST['skey2']) : urldecode(trim($_GET['skey2']));
		$skey3 = isset($_POST['skey3']) ? trim($_POST['skey3']) : urldecode(trim($_GET['skey3']));
		$skey4 = isset($_POST['skey4']) ? trim($_POST['skey4']) : urldecode(trim($_GET['skey4']));
		$skey5 = isset($_POST['skey5']) ? trim($_POST['skey5']) : urldecode(trim($_GET['skey5']));
	       
        $where = "1 ";
        if ($skey) {
            $where .= " AND channel_name like '%" . $skey . "%'";
        }
		if ($skey2) {
            $where .= " AND channel_sn like '%" . $skey2 . "%'";
        }
		if ($skey3 && $skey4) {
            $where .= " AND DATE_FORMAT(channel_create_time,'%Y-%m-%d')  BETWEEN '".$skey3."' AND '".$skey4."'";
        }


        load_file('page_class', LIB_PATH);
        $MyPage = new Mypage($db);
		$field = '*';

		
        $data_ary = $MyPage->from_sql($field, $main->get_table_name(), $where, "channel_sn", intval($_GET['cpage']), "20");
		//拼接渠道的id
		if($data_ary){
			foreach($data_ary as $k=>$v){
				$channel_sn .=",".$v['channel_sn'];				
			}			
		}
		$channel_sn = trim($channel_sn,",");//渠道的id	
		//$db->debug(1,1);
		$install_count = $main->get_install_count($channel_sn);	//安装的
        //dump($install_count);
		$reg_count = $main->get_reg_count($channel_sn); //注册的用户
		$active_count = $main->get_active_count($channel_sn);//手机验证通过的用户				
		
		//dump($reg_count);
	
		//将获得注册数的二维数组转化为以渠道id为键值的一维数组
		if($install_count){
			$install_ary = array();
			foreach($install_count as $k=>$v){
				$install_ary[$v['imei_channel_sn']] = $v['num'];
				$install_ary['total'] += $v['num'];
			}
		}
		/*
		//将获得验证数的二维数组转化为以渠道id为键值的一维数组
		if($reg_count){
			$reg_ary = array();
			foreach($reg_count as $k=>$v){
				$reg_ary[$v['imei_channel_sn']] = $v['num'];				
				$reg_ary['total'] += $v['num'];
			}
		}
			
		//将获得激活数的二维数组转化为以渠道id为键值的一维数组
		if($active_count){
			$active_ary = array();
			foreach($active_count as $k=>$v){
				$active_ary[$v['imei_channel_sn']] = $v['num'];				
				$active_ary['total'] += $v['num'];
			}
		}	*/	
		//dump($active_ary);
		//注册数和激活数拼接到列表中	
		if($data_ary){
			foreach($data_ary as $k=>$v){
				if($install_ary[$v['channel_sn']]){
					$data_ary[$k]['install_count'] = $install_ary[$v['channel_sn']];
				}	
								
				/*
				if($reg_ary[$v['channel_sn']]){
					$data_ary[$k]['reg_count'] = $reg_ary[$v['channel_sn']];
				}
				
				if($active_ary[$v['channel_sn']]){
					$data_ary[$k]['active_count'] = $active_ary[$v['channel_sn']];
				}	
				*/			
			}
		}		     
		$page = $MyPage->get_page($data, $cf_self_page . '?dpt='.$cf_dpt.'&skey=' . $skey . '&skey2=' . $skey2 . '&skey3=' . $skey3 . '&skey4=' . $skey4 . '&skey5=' . $skey5);
		
	break;
	case "add":
		$aid = isset($_POST['aid']) ? intval($_POST['aid']) : intval($_GET['aid']);			
		if($cf_ispost){
			//检测渠道号是否存在
			$sn = trim($_POST['channel_sn']);
			$main->check_sn($sn);					
			$data = $db->getFormData($main->get_table_name(),$_POST);
			$data['channel_create_time'] = date("Y-m-d H:i:s"); 
			//生成二维码
            $code_name = "/public/upload/apkimage/coship_".$data['channel_sn'].".png";      //二维码图片保存路径
            $logo_file = G_LOGO;                                                            //加在二维码中间的logo图片
            $value = SITE_ADDR.trim($data['channel_apk_url'],'/');                          //二维码保存的信息
			$output_file = ROOT_PATH.trim($code_name,'/');
            $src_file = ROOT_PATH."public/upload/apk/coship_".$data['channel_sn'].".apk"; 
			if(!is_file($src_file)){
				//my_jump('安装包不存在或没上传成功');
			}
			
			$res = create_qrcode($value,$output_file,true,$logo_file);
            $data['channel_code_img'] = $code_name;
			//dump($data);
			//$db->debug(1,1);
			$result = $main->insert_data($data);
			if($result){
				//更新申请状态
				if($aid){

					$data2['apply_status'] = "1";
					$where = "apply_id =".$aid;
					$main->update_apply_data($data2,$where);
				}
				
				//$more = $_SERVER['HTTP_REFERER'] ? '<br ><a class="edit" onclick="my_close()" target="main" href="'.$_SERVER['HTTP_REFERER'].'">[继续添加]</a>' : '';
				//$return_page = $cf_self_page."?dpt=".$cf_dpt; 
				$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
				unset($_SESSION['pre_url']);
				
				my_jump('添加成功'.$more,'',$return_page,3);
			}else{
				my_jump('添加失败');
			}
		}else{		
			$_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
			$row = $main->get_apply_info($aid);						
			$smarty->mypage($cf_controller,$cf_action,0);
			if($row){
				$row['channel_user_id'] = $row['apply_uid'];
				$row['channel_user_name'] = $row['apply_user'];
			}			
		}

    break;
	
	case "edit":
		if($cf_ispost){					
			$data = $db->getFormData($main->get_table_name(),$_POST);
			$data['channel_edit_time'] = date("Y-m-d");
            $row = $main->get_data_row($id);
			$code_name = "/public/upload/apkimage/coship_".$data['channel_sn'].".png";       //二维码图片保存路径
			$output_file = ROOT_PATH.trim($code_name,'/');
			$src_file = ROOT_PATH."public/upload/apk/coship_".$data['channel_sn'].".apk"; 
			if(!is_file($src_file)){
				//my_jump('安装包不存在或没上传成功');
			}
			$data['channel_file_size']	= get_file_size($src_file);
			if(!is_file($output_file)){
                //生成二维码
               // $code_name = UPLOAD_PATH."apkimage/coship_".$data['channel_sn'].".png";       //二维码图片保存路径
                $logo_file = G_LOGO;                                                            //加在二维码中间的logo图片
                $value = SITE_ADDR.trim($data['channel_apk_url'],'/');                          //二维码保存的信息
                $res = create_qrcode($value,$output_file,true,$logo_file);
                $data['channel_code_img'] = $code_name;
            }
			//$db->debug(1,1);
			$result = $main->update_data($data,"channel_id='".$id."'");
			if($result){
				//$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
                $return_page = $cf_self_path.'-act_index.htm?dpt='.$cf_dpt;

				unset($_SESSION['pre_url']);
				my_jump('修改成功','',$return_page,1);
            }else{
				my_jump('修改失败');
            }
		}else{
			$smarty->mypage($cf_controller,$cf_action,0);	
			$_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';		
			$row = $main->get_data_row($id);	
					
			if(!$row){
				my_jump('渠道信息不存在!');
			}	
			//dump($row);		
		}
    break;

	case "del":		
		
		$result = $main->del_data("channel_id='$id'");		
		if($result){			
			$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
			my_jump('删除成功','',$return_page,1);
		}else{
			my_jump('删除失败');
		}
	break;
		
	case "data":			
		$smarty->mypage($cf_controller, $cf_action, 0);	
		$sn = isset($_POST['sn']) ? trim($_POST['sn']) : urldecode(trim($_GET['sn']));
		$imei = isset($_POST['imei']) ? trim($_POST['imei']) : urldecode(trim($_GET['imei']));	
		$type = isset($_POST['type']) ? trim($_POST['type']) : urldecode(trim($_GET['type']));             
        $skey = isset($_POST['skey']) ? trim($_POST['skey']) : urldecode(trim($_GET['skey']));
		$skey2 = isset($_POST['skey2']) ? trim($_POST['skey2']) : urldecode(trim($_GET['skey2']));
		$skey3 = isset($_POST['skey3']) ? trim($_POST['skey3']) : urldecode(trim($_GET['skey3']));
		$skey4 = isset($_POST['skey4']) ? trim($_POST['skey4']) : urldecode(trim($_GET['skey4']));
		$skey5 = isset($_POST['skey5']) ? trim($_POST['skey5']) : urldecode(trim($_GET['skey5']));
		$skey6 = isset($_POST['skey6']) ? trim($_POST['skey6']) : urldecode(trim($_GET['skey6'])); 
		$skey7 = isset($_POST['skey7']) ? trim($_POST['skey7']) : urldecode(trim($_GET['skey7'])); 
		$skey8 = isset($_POST['skey8']) ? trim($_POST['skey8']) : urldecode(trim($_GET['skey8']));
        $skey9 = isset($_POST['skey9']) ? trim($_POST['skey9']) : urldecode(trim($_GET['skey9']));
        $skey10 = isset($_POST['skey10']) ? trim($_POST['skey10']) : urldecode(trim($_GET['skey10']));
        $skey11 = isset($_POST['skey11']) ? trim($_POST['skey11']) : urldecode(trim($_GET['skey11']));
        $skey12 = isset($_POST['skey12']) ? trim($_POST['skey12']) : urldecode(trim($_GET['skey12']));
        $where = "imei_channel_sn = '".$sn."'";		
		
		
		 if ($imei) {
            $where .= " AND imei_code like '%" . $imei . "%'";
        }
		
        if ($skey) {
            $where .= " AND imei_user like '%" . $skey . "%'";
        }
		if ($skey2) {
            $where .= " AND imei_reg_phone like '%" . $skey2 . "%'";
        }
		if ($skey3 && $skey4) {
           $where .= " AND DATE_FORMAT(imei_reg_time,'%Y-%m-%d') BETWEEN '".$skey3."' AND '".$skey4."'";
        }
		if ($skey5 && $skey6) {
           $where .= " AND DATE_FORMAT(imei_first_open,'%Y-%m-%d') BETWEEN '".$skey5."' AND '".$skey6."'";
        }
		if ($skey7 && $skey8) {
           $where .= " AND DATE_FORMAT(imei_last_open,'%Y-%m-%d') BETWEEN '".$skey7."' AND '".$skey8."'";
        }
        if ($skey9 && $skey10) {
            $where .= " AND DATE_FORMAT(imei_active_time,'%Y-%m-%d') BETWEEN '".$skey9."' AND '".$skey10."'";
        }
        if ($skey11 && $skey12) {
            $where .= " AND DATE_FORMAT(imei_back_time,'%Y-%m-%d') BETWEEN '".$skey11."' AND '".$skey12."'";
        }
		
		//$db->debug(1);
        load_file('page_class', LIB_PATH);
        $MyPage = new Mypage($db);
		//$field = 'data_imei.*,data_user.user_phone_province,user_phone_city,user_phone_supplier';
		//$tabel = "data_imei left join data_user on imei_reg_phone = user_phone and imei_reg_phone > '' ";
		$field = '*';
		$tabel = "data_imei";
		switch($type){
			case "install":
				$where .= "";
			break;
			case "reg":
				$where .= "AND imei_uid>'0'";
			break;
			case "active":
				$where .= "AND imei_reg_phone>'' ";
				$tabel = "data_imei left join data_user on imei_reg_phone = user_phone and imei_reg_phone > '' and user_check = '1' ";
			break;
		}			
        $data_ary = $MyPage->from_sql($field, $tabel, $where, "imei_id DESC", intval($_GET['cpage']), "20");			
		require_once(INFO_PATH.'cf_var_area.php');
		if($data_ary){
			foreach($data_ary as $k=>$v){
				$data_ary[$k]['phone_info'] = $cf_var_area[$v['user_phone_province']]['title'].$cf_var_area[$v['user_phone_province']]['child'][$v['user_phone_city']]['title'].$v['user_phone_supplier'];			
			}
		}
		//echo 	$MyPage->get_sql();	
		$row = $main->get_data_by_sn($sn);
		$page = $MyPage->get_page($data, $cf_self_page . '?act=data&type='.$type.'&sn='.$sn.'&dpt=' . $cf_dpt .'&skey=' . $skey . '&skey2=' . $skey2 . '&skey3=' . $skey3 . '&skey4=' . $skey4 . '&skey5=' . $skey5 . '&skey6=' . $skey6 . '&skey7=' . $skey7 . '&skey8=' . $skey8. '&skey9=' . $skey9. '&skey10=' . $skey10 . '&skey11=' . $skey11 .'&skey12=' . $skey12 .'&imei=' . $imei);
    break;
    case "count":
        $smarty->mypage("channel_count",$cf_action,0);
        $skey = isset($_POST['skey']) ? trim($_POST['skey']) : urldecode(trim($_GET['skey']));
        $skey2 = isset($_POST['skey2']) ? trim($_POST['skey2']) : urldecode(trim($_GET['skey2']));
        $skey3 = isset($_POST['skey3']) ? trim($_POST['skey3']) : urldecode(trim($_GET['skey3']));
        $skey4 = isset($_POST['skey4']) ? trim($_POST['skey4']) : urldecode(trim($_GET['skey4']));
        $skey5 = isset($_POST['skey5']) ? trim($_POST['skey5']) : urldecode(trim($_GET['skey5']));

        $where = "1";
        if ($skey) {
            $where .= " AND channel_name like '%" . $skey . "%'";
        }
        if ($skey2) {
            $where .= " AND channel_sn like '%" . $skey2 . "%'";
        }
        if ($skey3 && $skey4) {
            $where .= " AND DATE_FORMAT(channel_create_time,'%Y-%m-%d')  BETWEEN '".$skey3."' AND '".$skey4."'";
        }
        if ($skey5) {
            $where .= " AND channel_user_name like '%" . $skey5 . "%'";
        }

        $field = '*';
        $table = $main->get_table_name();
        $channel_data = $main->get_data_array($where,"channel_sn",0,$field);

        //拼接渠道的id
        if($channel_data){
            foreach($channel_data as $k=>$v){
                $channel_sn .=",".$v['channel_sn'];
            }
        }
        $channel_sn = trim($channel_sn,",");//渠道的id
        $install_count = $main->get_install_count($channel_sn);	//安装的
        $reg_count = $main->get_reg_count($channel_sn); //注册的用户
        $active_count = $main->get_active_count($channel_sn);//手机验证通过的用户

        //将获得注册数的二维数组转化为以渠道id为键值的一维数组
        if($install_count){
            foreach($install_count as $k=>$v){
                $row['install_total'] += $v['num'];
            }
        }
        //将获得验证数的二维数组转化为以渠道id为键值的一维数组
        if($reg_count){
            foreach($reg_count as $k=>$v){
                $row['reg_total'] += $v['num'];
            }
        }
        //将获得激活数的二维数组转化为以渠道id为键值的一维数组
        if($active_count){
            foreach($active_count as $k=>$v){
                $row['active_total'] += $v['num'];
            }
        }
        //dump($data_ary);

        break;

	default:
		my_jump('非法操作!');
	break;
}

$smarty->assign(array(
	'id'=>$id,
	'aid'=>$aid,
	'rid'=>$rid,
	'row'=>htmlchar($row),
	'data_ary'=>$data_ary,	
	'page'=>$page,
	'apkimg_path'=>$apkimg_path,
	'skey' => $skey,
	'skey2' => $skey2,
	'skey3' => $skey3,
	'skey4' => $skey4,
	'skey5' => $skey5,
	'skey6' => $skey6,
	'skey7' => $skey7,
	'skey8' => $skey8,
    'skey9' => $skey9,
    'skey10' => $skey10,
    'stype' => $stype,
	'type' => $type,
	'sn' => $sn,
	'imei' => $imei,
	'news_sort' => $news_sort,
	'cf_action' => $cf_action,
	'sessionid'=>session_id(),

));
$smarty->mydisplay();

?>